package cn.csg.iotplatform.module.system.dal.mysql.permission;

import cn.csg.iotplatform.framework.common.pojo.PageResult;
import cn.csg.iotplatform.framework.mybatis.core.dataobject.BaseDO;
import cn.csg.iotplatform.framework.mybatis.core.mapper.BaseMapperX;
import cn.csg.iotplatform.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.csg.iotplatform.module.system.controller.admin.permission.vo.role.RolePageReqVO;
import cn.csg.iotplatform.module.system.controller.admin.permission.vo.role.UserRolesReqDto;
import cn.csg.iotplatform.module.system.controller.admin.permission.vo.role.UserRolesRespVo;
import cn.csg.iotplatform.module.system.dal.dataobject.permission.RoleDO;
import cn.csg.iotplatform.module.system.dal.dataobject.permission.UserRolesDto;
import cn.csg.iotplatform.module.system.dal.dataobject.permission.UserRolesVo;
import cn.csg.iotplatform.module.system.dal.dataobject.permission.vo.RoleVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.lang.Nullable;

import java.util.Collection;
import java.util.List;

@Mapper
public interface RoleMapper extends BaseMapperX<RoleDO> {

    default PageResult<RoleDO> selectPage(RolePageReqVO reqVO) {

        LambdaQueryWrapperX<RoleDO> roleDOLambdaQueryWrapperX = new LambdaQueryWrapperX<RoleDO>()
                .likeIfPresent(RoleDO::getName, reqVO.getName())
                .likeIfPresent(RoleDO::getCode, reqVO.getCode())
                .eqIfPresent(RoleDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(BaseDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(RoleDO::getId);

        return selectPage(reqVO, roleDOLambdaQueryWrapperX);
    }

    default RoleDO selectByName(String name) {
        return selectOne(RoleDO::getName, name);
    }

    default RoleDO selectByCode(String code) {
        return selectOne(RoleDO::getCode, code);
    }

    default List<RoleDO> selectListByStatus(@Nullable Collection<Integer> statuses) {
        return selectList(RoleDO::getStatus, statuses);
    }

    default List<RoleDO> selectListByRoleIds(@Nullable Collection<Long> roleIds) {
        return selectList(RoleDO::getId, roleIds);
    }

    List<RoleVO> queryRolePage(@Param("reqVO") RolePageReqVO reqVO);

    Long queryRoleTotal(@Param("reqVO") RolePageReqVO reqVO);

    List<UserRolesVo> getUserRoleList(@Param("dto") UserRolesDto dto);

    Long getUserRoleTotal(@Param("dto") UserRolesDto dto);

    List<UserRolesRespVo> queryUserRolesPage(@Param("dto") UserRolesReqDto dto);

    Long queryUserRolesTotal(@Param("dto") UserRolesReqDto dto);

    Long selectDataScope4RoleCount(@Param("userId")Long userId);

    Long selectSuperAdminRoleByUserId(@Param("userId")Long userId);

    IPage<UserRolesRespVo> queryUserOwnerConfigRolePage(Page<UserRolesRespVo> page, @Param("userId") Long userId, @Param("editUserId") Long editUserId, @Param("description") String description, @Param("tableNum") Long tableNum);
}
